package com.cnbot.toolkit.sharepref;

import android.content.Context;
import android.content.SharedPreferences;

import com.cnbot.toolkit.context.AppContextUtil;

import java.util.Map;

/**
 *  @项目名：  EcologyRobot4
 *  @包名：    com.cnbot.ecologyrobot2.base
 *  @文件名:   IRelease
 *  @创建者:   ww
 *  @创建时间:  2019/1/18 11:32
 *  @描述：    偏好工具类
 */
public final class SPUtils {

	public static final String FILE_NAME = "robot_shared_pref";

	private SPUtils() {}

	private static SharedPreferences.Editor editor(Context context) {

		SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		return sp.edit();
	}

	private static SharedPreferences.Editor editor() {

		return editor(AppContextUtil.getApplicationContext());
	}

	private static SharedPreferences sp(Context context) {

		return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
	}

	private static SharedPreferences sp() {

		return sp(AppContextUtil.getApplicationContext());
	}

	/**
	 * 移除指定的偏好设置
	 * @param key
	 */
	public static void remove(String key) {
		SharedPreferences.Editor editor = sp().edit();
		editor.remove(key);
		editor.commit();
	}

	/**
	 * 清除所有的偏好设置
	 */
	public static void clear() {
		SharedPreferences.Editor editor = sp().edit();
		editor.clear();
		editor.commit();
	}

	/**
	 * 是否包含某一个指定的偏好
	 * @param key
	 * @return
	 */
	public static boolean contains(String key) {
		return sp().contains(key);
	}

	/**
	 * 获取所有的偏好
	 * @return
	 */
	public static Map<String, ?> getAll() {
		return sp().getAll();
	}

	/**
	 * 保存boolean类型的偏好
	 *
	 * @param key
	 * @param value
	 */
	public static void putBoolean(String key, boolean value) {
		SharedPreferences.Editor editor = editor();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 * 保存String类型的偏好
	 *
	 * @param key
	 * @param value
	 */
	public static void putString(String key, String value) {
		SharedPreferences.Editor editor = editor();
		editor.putString(key, value);
		editor.commit();
	}

    /**
     * 保存int类型的偏好
     *
     * @param key
     * @param value
     */
    public static void putInt(String key, int value) {
        SharedPreferences.Editor editor = editor();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 保存long类型的偏好
     *
     * @param key
     * @param value
     */
    public static void putLong(String key, long value) {
        SharedPreferences.Editor editor = editor();
        editor.putLong(key, value);
        editor.commit();
    }



	/**
	 * 保存Float类型的偏好
	 * @param key
	 * @param value
	 */
	public static void putFloat(String key, float value) {
		SharedPreferences.Editor editor = editor();
		editor.putFloat(key, value);
		editor.commit();
	}

	/**
	 * 获取String类型的偏好
	 */
	public static String getString(String key) {
		SharedPreferences sp = sp();
		return sp.getString(key, "");

	}

	/**
	 * 获取String类型的偏好
	 * @param key 
	 * @param defValue 默认值
	 */
	public static String getString(String key, String defValue) {
		SharedPreferences sp = sp();
		return sp.getString(key, defValue);

	}

	/**
	 * 获取Boolean类型的偏好
	 */
	public static boolean getBoolean(String key) {
		SharedPreferences sp = sp();
		return sp.getBoolean(key, false);

	}

	/**
	 * 获取Boolean类型的偏好
	 * @param key
	 * @param defValue 默认值
	 */
	public static boolean getBoolean(String key, boolean defValue) {
		SharedPreferences sp = sp();
		return sp.getBoolean(key, defValue);

	}

	/**
	 * 获取Float类型的偏好
	 */
	public static float getFloat(String key) {
		SharedPreferences sp = sp();
		return sp.getFloat(key, 0f);

	}

	/**
	 * 获取Float类型的偏好
	 * @param key
	 * @param defValue 默认值
	 */
	public static float getFloat(String key, float defValue) {
		SharedPreferences sp = sp();
		return sp.getFloat(key, defValue);

	}

    /**
     * 获取int类型的偏好
     */
    public static int getInt(String key) {
        SharedPreferences sp = sp();
        return sp.getInt(key, 0);

    }

    /**
     * 获取int类型的偏好
     * @param key
     * @param defValue 默认值
     */
    public static int getInt(String key, int defValue) {
        SharedPreferences sp = sp();
        return sp.getInt(key, defValue);

    }

    /**
     * 获取long类型的偏好
     */
    public static long getLong(String key) {
        SharedPreferences sp = sp();
        return sp.getLong(key, 0);

    }

    /**
     * 获取long类型的偏好
     * @param key
     * @param defValue 默认值
     */
    public static long getLong(String key, long defValue) {
        SharedPreferences sp = sp();
        return sp.getLong(key, defValue);

    }

}
